﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
    /// <summary>
    /// Interaction logic for InputBox2.xaml
    /// </summary>
    public partial class InputBox2 : UserControl
    {
        public InputBox2()
        {
            InitializeComponent();
            inputTxtField.Text = "";
            this.SARErrorMessage = "这是输入错误提示信息！请删除InPutBox2控件的构造方法中的错误信息代码段！";
        }
        //定义事件
        public event MouseButtonEventHandler SARIncBtnClick = null;
        public event MouseButtonEventHandler SARDecBtnClick = null;
        public event TextChangedEventHandler SARTextChange = null;


        private double increment = 1;

        public double SARIncrement
        {
            get
            {
                return increment;
            }
            set
            {
                increment = value;
            }
        }



        private double contentTxt = 0;

        //ContentTxt属性，输入框内容
        public double SARContentTxt
        {
            get
            {
                if (inputTxtField.Text != "")
                {
                    contentTxt = Convert.ToDouble(inputTxtField.Text);
                    
                }
                return contentTxt;
            }
            set
            {
                contentTxt = value;
                inputTxtField.Text = contentTxt.ToString();
            }
           
        }

        private void incBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.incBtn.Opacity = 0.4;
                if (inputTxtField.Text != "")
                {
                    contentTxt = Convert.ToDouble(inputTxtField.Text);
                    contentTxt += increment;
                    inputTxtField.Text = contentTxt.ToString();
                }
                else
                {
                }
                if (SARIncBtnClick != null)
                {
                    SARIncBtnClick.Invoke(this, e);
                }
            }
            catch (System.Exception ex)
            {
            }
            
        }

        private void decBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.decBtn.Opacity = 0.4;

                if (inputTxtField.Text != "")
                {
                    contentTxt = Convert.ToDouble(inputTxtField.Text);
                    contentTxt -= increment;

                    inputTxtField.Text = contentTxt.ToString();
                }
                else
                {
                }
                if (SARDecBtnClick != null)
                {
                    SARDecBtnClick.Invoke(this, e);
                }
            }
            catch (System.Exception ex)
            {
            	
            }
            
        }

        private void incBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.incBtn.Opacity = 0;
        }    

        private void decBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.decBtn.Opacity = 0;
        }

        private void incBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.incBtn.Opacity = 0;
        }

        private void decBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.decBtn.Opacity = 0;
        }

        private void inputTxtField_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                contentTxt = Convert.ToDouble(inputTxtField.Text);
            }
            catch (System.Exception ex)
            {
                contentTxt = 0;
            }
            
            if (SARTextChange != null)
            {
                SARTextChange.Invoke(this, e);
            }
        }

        private string errorMessage = "";
        /// <summary>
        /// 错误消息，代替ErrorProvider的功能，如果设置""即可消除
        /// </summary>
        public string SARErrorMessage
        {
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    errorMessage = "";
                    imgError.Visibility = Visibility.Hidden;
                }
                else
                {
                    errorMessage = value;
                    imgError.Visibility = Visibility.Visible;
                    imgError.ToolTip = value;
                }
            }
            get
            {
                return errorMessage;
            }
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                this.Opacity = 1;
            }
            else
            {
                this.Opacity = 0.5;
            }
        }
    }
}
